//
//  Index.swift
//  skincare
//
//  Created by whu125 on 2023/5/18.
//

import Moya

enum IndexService {
    case getPost
}

extension IndexService: TargetType {

    var baseURL: URL {
        return URL(string: "http://106.14.226.26:8080/fun-skincare")!
    }

    var path: String {
        switch self {
        case .getPost:
            return "/post"
        }
    }

    var method: Method {
        return .get
    }

    var sampleData: Data {
        return Data()
    }

    var task: Task {
        switch self {
        case .getPost:
            return .requestPlain
        }
        //表示没有参数的网络请求
        //case .getUsers(let userId):
       // return .requestParameters(parameters: ["userId": userId], encoding: JSONEncoding.default)
    }

    var headers: [String : String]? {
        return ["Content-type": "application/json"]
    }
}
let indexendpoint = { (target: IndexService) -> Endpoint in
    let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
    let token = getToken()
    return defaultEndpoint.adding(newHTTPHeaderFields: ["Authorization": "Bearer \(token)"])
}

let index_provider = MoyaProvider<IndexService>(endpointClosure: indexendpoint)
